package com.sws.device.service;

import com.sws.common.utils.access_tokenutil.util.AccessTokenUtil;
import com.sws.device.domain.DevDevice;
import com.sws.device.mapper.DevDeviceMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 设备IP解析器服务
 * 用于根据设备SN获取设备IP地址
 */
@Service
public class DeviceIpResolverService implements AccessTokenUtil.DeviceIpResolver {

    private static final Logger log = LoggerFactory.getLogger(DeviceIpResolverService.class);

    @Autowired
    private DevDeviceMapper devDeviceMapper;

    /**
     * 根据设备序列号解析IP地址
     *
     * @param sn 设备序列号
     * @return 设备IP地址
     */
    @Override
    public String resolveIpBySn(String sn) {
        if (!StringUtils.hasLength(sn)) {
            log.warn("设备SN为空，无法解析设备IP地址");
            return null;
        }

        log.info("正在根据SN[{}]查询设备IP地址", sn);
        DevDevice device = devDeviceMapper.selectDevDeviceBySn(sn);
        if (device != null) {
            if (StringUtils.hasLength(device.getIp())) {
                log.info("成功解析设备SN[{}]的IP地址: {}", sn, device.getIp());
                return device.getIp();
            } else {
                log.warn("设备SN[{}]的IP地址为空", sn);
            }
        } else {
            log.warn("未找到SN为[{}]的设备", sn);
        }
        
        return null;
    }
} 